//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.dao;

import cn.edu.sdjzu.xg.kcsj.domain.authority.Actor;
import cn.edu.sdjzu.xg.kcsj.service.StudentService;
import cn.edu.sdjzu.xg.kcsj.service.TeacherService;
import util.JdbcHelper;

import java.sql.*;

public final class ActorDao {
    private static final ActorDao actorDao = new ActorDao();
    private ActorDao() {
    }

    public static ActorDao getInstance() {
        return actorDao;
    }

    public Actor find(Connection connection, int id) throws SQLException {
        Actor desiredActor = null;
        desiredActor = StudentService.getInstance().find(id);
        if (desiredActor != null) {
            return desiredActor;
        }
        desiredActor = TeacherService.getInstance().find(id);
        return desiredActor;
    }
    public int add(Connection connection) throws SQLException {
        //根据连接对象准备语句对象，如果SQL语句为多行，注意语句不同部分之间要有空格
        PreparedStatement preparedStatement_actor = connection.prepareStatement("INSERT INTO actor () VALUES ()", Statement.RETURN_GENERATED_KEYS);
        int affectedRowNum = preparedStatement_actor.executeUpdate();
        int idOfAddedActor = 0;
        ResultSet resultSet = preparedStatement_actor.getGeneratedKeys();
        if (resultSet.next()) {
            idOfAddedActor = resultSet.getInt(1);
        }
        JdbcHelper.close(null,preparedStatement_actor);
        return idOfAddedActor;
    }

}
